package com.shure.modules.docs.controller;

import java.io.File;
import java.io.IOException;
import java.util.List;

import org.apache.commons.io.FileUtils;
import org.apache.commons.io.FilenameUtils;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.shure.common.query.condition.Condition;
import com.shure.modules.docs.entity.Files;
import com.shure.modules.docs.query.FilesQuery;
import com.shure.modules.docs.service.FilesService;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;


@RestController
@RequestMapping("files")
public class FilesController {

    private final FilesService filesService;

    public FilesController(FilesService filesService) {
        this.filesService = filesService;
    }

    @GetMapping("list")
    public ResponseEntity<List<Files>> list(FilesQuery query) {
        LambdaQueryWrapper<Files> wrapper = Condition.getLambdaQueryWrapper1(query);
        wrapper.orderByDesc(Files::getUpdateAt);
        return ResponseEntity.ok(filesService.list(wrapper));
    }

    @GetMapping("{id}")
    public ResponseEntity<Files> detail(@PathVariable("id") Long id) {
        return ResponseEntity.ok(filesService.getById(id));
    }

    @PostMapping("upload")
    public ResponseEntity<Files> upload(@RequestParam("file") MultipartFile file, @RequestParam("bizId") Long bizId) throws IOException {
        
        String fileName = file.getOriginalFilename();
        String fileFolder = "./data/files/" + bizId + "/";
        
        // 保存文件到磁盘，先判断文件目录存在不
        if (!new File(fileFolder).exists()) {
            new File(fileFolder).mkdirs();
        }

        String filePath = fileFolder + fileName;
        file.transferTo(new File(filePath));

        Files files = new Files();
        files.setFileSize(file.getSize());
        files.setFileName(fileName);
        files.setFileExt(FilenameUtils.getExtension(fileName));
        files.setFilePath(filePath);
        files.setFileUrl("");
        filesService.save(files);
        return ResponseEntity.ok(files);
    }

    @PutMapping("{id}")
    public ResponseEntity<Files> update(@PathVariable("id") Long id, @RequestBody Files files) {
        files.setId(id);
        filesService.updateById(files);
        return ResponseEntity.ok(files);
    }

    @DeleteMapping("{id}")
    public ResponseEntity<Void> delete(@PathVariable("id") Long id) {
        filesService.removeById(id);
        return ResponseEntity.noContent().build();
    }


    
}
